package com.fattiger.framework.util;

import java.text.DecimalFormat;

import java.math.BigDecimal;

public class MathUtil {

	public static void main(String[] args){
		System.out.println(switchYuanToFen("100"));
	}
	public static String formatFloarby2(float f ){
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(2);
		df.setMinimumFractionDigits(0);
		return df.format(f);
	}
	
	
	public static int switchYuanToFen(String yuan) {
		BigDecimal bd = new BigDecimal(yuan);
		bd = bd.multiply(new BigDecimal(100));
		return bd.intValue();
	}
	
	/**
	 * 分转元
	 * @param 
	 * @return
	 */
	public static String switchFenToYuan(String fen){ 
		if(!StringUtil.isEmpty(fen)){
			double f =Double.valueOf(fen);
			double yuan = f/100;   
			DecimalFormat df = new DecimalFormat("0.00");
		    String Y=df.format(yuan);
		    return Y;
		}
		return null;
	}
	/**
	 * 分转元
	 * @param 
	 * @return
	 */
	public static String switchFenToYuanInt(String fen){ 
		if(!StringUtil.isEmpty(fen)){
			double f =Double.valueOf(fen);
			double yuan = f/100;   
			DecimalFormat df = new DecimalFormat("0");
			String Y=df.format(yuan);
			return Y;
		}
		return null;
	}
	/**
	 * 将流量单位“KB”转化为“MB”带两位小数
	 * @param KB 
	 * @return MB
	 */
	public static String switchKbToMb(String KB){ 
		if(!StringUtil.isEmpty(KB)){
			double kb=Double.valueOf(KB);
			double mb = kb/1024.0;   
			DecimalFormat df = new DecimalFormat("0.00");
		    String MB=df.format(mb);
		    return MB.replace(".00", "");
		}
		return null;
	}
	/**
	 * 返回整数mb
	 * @param KB
	 * @return
	 */
	public static String switchKbToMbInt(String KB){ 
		if(!StringUtil.isEmpty(KB)){
			double kb=Double.valueOf(KB);
			double mb = kb/1024.0;   
			DecimalFormat df = new DecimalFormat("0");
			String MB=df.format(mb);
			return MB;
		}
		return null;
	}
	
	/**
	 * 将流量单位“MB”转化为“KB”
	 * @param MB 
	 * @return KB
	 */
	public static String switchMbToKb(String MB){ 
		if(!StringUtil.isEmpty(MB)){
			double mb=Double.valueOf(MB);
			int kb = (int)(mb*1024);   
			return String.valueOf(kb); 
		}
		return null;
	}	
}
